from time import ctime
from sklearn.externals import joblib
logFile = open('log.txt','a')
analysisf = open('debug.txt', 'a')

def getResultM(p, path, CH, PH, text_clf, subData):
	if p not in text_clf:
		try:
			text_clf[p] = joblib.load(path+str(p)+'.pkl')
		except:
			text_clf[p] =  None
	if text_clf[p] == None:
		classes = 0
		resultM = None
	else:
		resultM = text_clf[p].predict_log_proba(subData)
		classes = list(text_clf[p].steps[-1][-1].classes_)
	return [classes, resultM]

def printLog(s):
	print s
	print >>logFile, s
	logFile.flush()

def analysisLog(s):
	print s
	print >>analysisf, s
	analysisf.flush()

def NumPathMatched(predicted,labels):
	maxCnt = 0
	for l in labels:
		cnt = 0
		for i in range(min(len(predicted),len(l))):
			if predicted[i] == l[i]:
				cnt += 1
			else:
				break
		if cnt == len(l):
			return 9
		if cnt > maxCnt:
			maxCnt = cnt

	return maxCnt

def resultLog(predicted, labels):
	correct = [0] * 10
	printLog(logFile, 'L1 to L9 in %')
	for i in range(len(predicted)):
		ret = NumPathMatched(predicted[i][1:],labels[i])
		for l in range(0,ret+1):
			correct[l]+=1

	for i in range(1,10):
		printLog(logFile, str(round(float(correct[i])/correct[0]*100, 2)))
	printLog(logFile, '--------------')